package com.kvest.scanrecognizer.ui.activity;

import android.R;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.graphics.Color;
import android.os.Bundle;
import android.support.v4.content.LocalBroadcastManager;
import android.support.v7.app.ActionBarActivity;
import android.view.Window;
import com.bugsense.trace.BugSenseHandler;
import com.github.johnpersano.supertoasts.SuperToast;
import com.kvest.scanrecognizer.service.NetworkService;
import com.kvest.scanrecognizer.utility.Utils;

/**
 * Created with IntelliJ IDEA.
 * User: Kvest
 * Date: 10.09.13
 * Time: 19:10
 * To change this template use File | Settings | File Templates.
 */
public class BaseActivity extends ActionBarActivity {
    private static final String BUGSENS_APIKEY = "eecb73f7";

//    protected ShareActionProvider shareActionProvider;
    private ServiceStateBroadcastReceiver serviceStateBroadcastReceiver = new ServiceStateBroadcastReceiver();

    @Override
    public void onCreate(Bundle savedState) {
        super.onCreate(savedState);
        BugSenseHandler.initAndStartSession(this, BUGSENS_APIKEY);
        supportRequestWindowFeature(Window.FEATURE_INDETERMINATE_PROGRESS);
    }

    @Override
    public void onStart() {
        super.onStart();

        setSupportProgressBarIndeterminateVisibility(Utils.isServiceRunning(this, NetworkService.class));
        LocalBroadcastManager.getInstance(this).registerReceiver(serviceStateBroadcastReceiver, new IntentFilter(NetworkService.START_ACTION));
        LocalBroadcastManager.getInstance(this).registerReceiver(serviceStateBroadcastReceiver, new IntentFilter(NetworkService.STOP_ACTION));
        LocalBroadcastManager.getInstance(this).registerReceiver(serviceStateBroadcastReceiver, new IntentFilter(NetworkService.MESSAGE_ACTION));
    }

    @Override
    public void onStop() {
        super.onStop();

        LocalBroadcastManager.getInstance(this).unregisterReceiver(serviceStateBroadcastReceiver);
        setSupportProgressBarIndeterminateVisibility(false);
    }

//    @Override
//    public boolean onCreateOptionsMenu(Menu menu) {
//        // Inflate the menu
//        getMenuInflater().inflate(R.menu.base_menu, menu);
//
//        // Find the share item
//        MenuItem shareItem = menu.findItem(R.id.menu_share);
//
//        // Need to use MenuItemCompat to retrieve the Action Provider
//        shareActionProvider = (ShareActionProvider) MenuItemCompat.getActionProvider(shareItem);
//        shareActionProvider.setShareIntent(getDefaultIntent());
//
//        return super.onCreateOptionsMenu(menu);
//    }

    protected Intent getDefaultIntent() {
        Intent intent = new Intent(Intent.ACTION_SEND);
        intent.setType("text/plain");
        return intent;
    }

    protected void showMessage(int messageType, String text) {
        SuperToast superToast = new SuperToast(this);
        superToast.setDuration(SuperToast.DURATION_LONG);
        superToast.setAnimation(SuperToast.ANIMATION_SCALE);
        switch (messageType) {
            case NetworkService.MESSAGE_TYPE_INFO :
                superToast.setBackgroundResource(SuperToast.BACKGROUND_BLUE);
                superToast.setIconResource(R.drawable.ic_dialog_info, SuperToast.IconPosition.LEFT);
                break;
            case NetworkService.MESSAGE_TYPE_ERROR :
                superToast.setBackgroundResource(SuperToast.BACKGROUND_ORANGE);
                superToast.setIconResource(R.drawable.ic_dialog_alert, SuperToast.IconPosition.LEFT);
                break;
        }
        superToast.setTextColor(Color.WHITE);
        superToast.setTextSize(SuperToast.TEXTSIZE_MEDIUM);
        superToast.setText(text);
        superToast.show();
    }

    private class ServiceStateBroadcastReceiver extends BroadcastReceiver {
        @Override
        public void onReceive(Context context, Intent intent) {
            String action = intent.getAction();
            if (NetworkService.START_ACTION.equals(action)) {
                setSupportProgressBarIndeterminateVisibility(true);
            } else if (NetworkService.STOP_ACTION.equals(action)) {
                setSupportProgressBarIndeterminateVisibility(false);
            } else if (NetworkService.MESSAGE_ACTION.equals(action)) {
                showMessage(intent.getIntExtra(NetworkService.MESSAGE_TYPE_EXTRA, -1),
                            intent.getStringExtra(NetworkService.MESSAGE_TEXT_EXTRA));
            }
        }
    }
}
